﻿#ifndef FHMAINWINDOW_H
#define FHMAINWINDOW_H

#include <QMainWindow>
#include <QSqlDatabase>//连接数据库相关
#include <QSqlError>//数据库错误相关
#include <QSqlQuery>//执行sql语句相关
#include <QSqlRecord>
#include <QStandardItemModel>
#include <QStandardItem>

#include <QMessageBox>
#include <QDebug>//程序输出，相当于print
#include <QFileDialog>
#include <QDesktopServices>
#include <QProcess>
#include <QLabel>
#include <QFormLayout>
#include <QRadioButton>
#include <QButtonGroup>
#include <QComboBox>
#include <QSettings>

#include <QElapsedTimer>
#include <QTimer>
#include <QDateTime>
#include "entity.h"
#include "importbyhand.h"
#include "login.h"
#include "ui_fhmainwindow.h"  // 确保包含这个头文件
#include "database_home.h"
namespace Ui {
class FHMainWindow;
}

class FHMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit FHMainWindow(QWidget *parent = nullptr);
    ~FHMainWindow();

//重写closeEvent方法，实现关闭mainwindow的同时关闭非子类的showWidget
protected:
    void closeEvent(QCloseEvent *event) override {
        if(fhklSearchDialog){
            fhklSearchDialog->close();
        }
        if(sampleSearchDialog){
            sampleSearchDialog->close();
        }
        if(experimentSearchDialog){
            experimentSearchDialog->close();
        }
        if(showFhklDialog){
            showFhklDialog->close();
        }
        if(showSampleDialog){
            showSampleDialog->close();
        }
        QMainWindow::closeEvent(event);
    }

private slots:
    void on_action_fhkl_triggered();

    void on_fhkl_search_btn3_clicked();

    void on_action_insert_fhkl_triggered();

    void on_action_sample_triggered();

    void on_sample_search_btn3_clicked();

    void on_fhkl_search_btn1_clicked();

    void on_action_insert_sample_triggered();

    void on_action_syxx_triggered();

    void on_syxx_search_btn3_clicked();

    void on_action_insert_syxx_triggered();

    void on_sample_search_btn1_clicked();

    void on_syxx_search_btn1_clicked();

    void on_action_logout_triggered();

    void on_action_exit_triggered();

    void onSelectAllCheckBoxStateChanged(int state) {
        if (!state) {
            return;
        }
        int currentIndex = ui->fhtabWidget->currentIndex();  // 获取当前选项卡的索引
        qDebug() << "Current Tab Index:" << currentIndex;

        if (currentIndex == 0) {
            // 对第一个选项卡 (fhkl) 执行全选操作
            qDebug() << "fhkl_model->rowCount():" << fhkl_model->rowCount();
            if(fhkl_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                return;
            }
            for (int row = fhkl_model->rowCount() - 1; row >= 0; --row) {  // 从后往前
                QStandardItem* selectedItem = fhkl_model->item(row, 0);
                selectedItem->setCheckState(state ? Qt::Checked : Qt::Unchecked);
            }
            ui->fhkl_tableView->setModel(fhkl_model);  // 刷新视图

        } else if (currentIndex == 1) {
            // 对第二个选项卡 (sample) 执行全选操作
            qDebug() << "sample_model->rowCount():" << sample_model->rowCount();
            if(sample_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                return;
            }
            for (int row = sample_model->rowCount() - 1; row >= 0; --row) {  // 从后往前
                QStandardItem* selectedItem = sample_model->item(row, 0);
                selectedItem->setCheckState(state ? Qt::Checked : Qt::Unchecked);
            }
            ui->sample_tableView->setModel(sample_model);  // 刷新视图

        } else if (currentIndex == 2) {
            // 对第三个选项卡 (syxx) 执行全选操作
            qDebug() << "experiment_model->rowCount():" <<  experiment_model->rowCount();
            if(experiment_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                return;
            }
            for (int row = experiment_model->rowCount() - 1; row >= 0; --row) {  // 从后往前
                QStandardItem* selectedItem = experiment_model->item(row, 0);
                selectedItem->setCheckState(state ? Qt::Checked : Qt::Unchecked);
            }
            ui->syxx_tableView->setModel(experiment_model);  // 刷新视图
        }
    }

    void onSelectAllCheckBoxStateChangedPlus(int state) {
        int currentIndex = ui->fhtabWidget->currentIndex();  // 获取当前选项卡的索引
        qDebug() << "Current Tab Index:" << currentIndex;

        if (currentIndex == 0) {
            // 对第一个选项卡 (fhkl) 执行全选操作
            qDebug() << "fhkl_model->rowCount():" << fhkl_model->rowCount();
            if(fhkl_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                return;
            }
            for (int row = fhkl_model->rowCount() - 1; row >= 0; --row) {  // 从后往前
                QStandardItem* selectedItem = fhkl_model->item(row, 0);
                selectedItem->setCheckState(state ? Qt::Checked : Qt::Unchecked);
            }
            ui->fhkl_tableView->setModel(fhkl_model);  // 刷新视图

        } else if (currentIndex == 1) {
            // 对第二个选项卡 (sample) 执行全选操作
            qDebug() << "sample_model->rowCount():" << sample_model->rowCount();
            if(sample_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                return;
            }
            for (int row = sample_model->rowCount() - 1; row >= 0; --row) {  // 从后往前
                QStandardItem* selectedItem = sample_model->item(row, 0);
                selectedItem->setCheckState(state ? Qt::Checked : Qt::Unchecked);
            }
            ui->sample_tableView->setModel(sample_model);  // 刷新视图

        } else if (currentIndex == 2) {
            // 对第三个选项卡 (syxx) 执行全选操作
            qDebug() << "experiment_model->rowCount():" <<  experiment_model->rowCount();
            if(experiment_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                return;
            }
            for (int row = experiment_model->rowCount() - 1; row >= 0; --row) {  // 从后往前
                QStandardItem* selectedItem = experiment_model->item(row, 0);
                selectedItem->setCheckState(state ? Qt::Checked : Qt::Unchecked);
            }
            ui->syxx_tableView->setModel(experiment_model);  // 刷新视图
        }


//        QStandardItemModel *model = nullptr;

//        switch (currentIndex) {
//        case 0:
//            model = dynamic_cast<QStandardItemModel*>(ui->fhkl_tableView->model());
//            break;
//        case 1:
//            model = dynamic_cast<QStandardItemModel*>(ui->sample_tableView->model());
//            break;
//        case 2:
//            model = dynamic_cast<QStandardItemModel*>(ui->syxx_tableView->model());
//            break;
//        default:
//            qDebug() << "Unknown tab index.";
//            break;
//        }

//        if (!model) {
//            qDebug() << "Failed to get model for the current tab index.";
//            return;
//        }

//        // 更新全选复选框状态
//        int current_count = 0;

//        for (int row = 0; row < model->rowCount(); ++row) {
//            QStandardItem *checkItem = model->item(row, 0);
//            if (checkItem && checkItem->checkState() == Qt::Checked) {
//                current_count++;
//            }
//        }

//        qDebug() << " 当前的 currentCount ：" << current_count;

//        if(current_count == model->rowCount()) {
//            ui->selectall_checkBox->setCheckState(Qt::Checked);
//        } else {
//            ui->selectall_checkBox->setCheckState(Qt::Unchecked);
//        }
    }


    void onTakeSelectedBtnClicked() {

        if (ui->selectall_checkBox->checkState() || true) {
            int currentIndex = ui->fhtabWidget->currentIndex();  // 获取当前选项卡的索引
            qDebug() << "Current Tab Index:" << currentIndex;

            if (currentIndex == 0) {
                // 对第一个选项卡 (fhkl) 执行删除操作
                if(fhkl_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                    QMessageBox::warning(this, "警告", "未勾选任何行");
                    return;
                }
                qDebug() << "fhkl_model->rowCount():" << fhkl_model->rowCount();
                for (int row = fhkl_model->rowCount() - 1; row >= 0; --row) {  // 从后往前删除
                    QStandardItem* selectedItem = fhkl_model->item(row, 0);
                    if (selectedItem->checkState() == Qt::Checked) {
                        qDebug() << "Deleting row:" << row;
                        fhkl_model->takeRow(row);  // 删除行
                    }
                }
                ui->fhkl_tableView->setModel(fhkl_model);  // 刷新视图

            } else if (currentIndex == 1) {
                // 对第二个选项卡 (sample) 执行删除操作
                if(sample_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                    QMessageBox::warning(this, "警告", "未勾选任何行");
                    return;
                }
                qDebug() << "sample_model->rowCount():" << sample_model->rowCount();
                for (int row = sample_model->rowCount() - 1; row >= 0; --row) {  // 从后往前删除
                    QStandardItem* selectedItem = sample_model->item(row, 0);
                    if (selectedItem->checkState() == Qt::Checked) {
                        qDebug() << "Deleting row:" << row;
                        sample_model->takeRow(row);  // 删除行
                    }
                }
                ui->sample_tableView->setModel(sample_model);  // 刷新视图

            } else if (currentIndex == 2) {
                // 对第三个选项卡 (syxx) 执行删除操作
                if(experiment_model->headerData(0, Qt::Horizontal).toString() != "选择") {
                    QMessageBox::warning(this, "警告", "未勾选任何行");
                    return;
                }
                qDebug() << "experiment_model->rowCount():" <<  experiment_model->rowCount();
                for (int row = experiment_model->rowCount() - 1; row >= 0; --row) {  // 从后往前删除
                    QStandardItem* selectedItem = experiment_model->item(row, 0);
                    if (selectedItem->checkState() == Qt::Checked) {
                        qDebug() << "Deleting row:" << row;
                        experiment_model->takeRow(row);  // 删除行
                    }
                }
                ui->syxx_tableView->setModel(experiment_model);  // 刷新视图
            }
            update_model_count();
        }
    }

    void on_fhkltohome_btn_clicked();

    void on_sampletohome_btn_clicked();

    void on_syxxtohome_btn_clicked();

    void on_fhkl_search_btn2_clicked();

    void on_sample_search_btn2_clicked();

    void on_syxx_search_btn2_clicked();

    void on_exportselected_btn_clicked();

    void on_exportall_btn_clicked();

    void on_action_update_triggered();

    void on_action_delete_triggered();

    void on_deleteselected_btn_clicked();

    void on_action_dirpath_triggered();

private:
    Ui::FHMainWindow *ui;

    QWidget *fhklSearchDialog = nullptr; // 查询对话框指针
    QWidget *sampleSearchDialog = nullptr;
    QWidget *experimentSearchDialog = nullptr;

    QWidget *showFhklDialog = nullptr; // experiment表关联fhkl或sample表主键的对话框指针
    QWidget *showSampleDialog = nullptr;


    QSqlDatabase fhDatabase;// 直接使用对象而不是指针

    QStandardItemModel *fhkl_model;
    QStandardItemModel *sample_model;
    QStandardItemModel *experiment_model;

    FlyFireParticle fhkl_struct;
    SampleInfo sample_struct;
    ExperimentInfo experiment_struct;

    //void initFlyFireParticle(FlyFireParticle *particle);
    void showFhklInTableView(FlyFireParticle *particle);
    void update_model_count();
    int update_selected_count();

    // 建表
    bool checkAndCreateFhklTable(QSqlDatabase *fhDatabase);
    bool checkAndCreateSampleTable(QSqlDatabase *sampleDatabase);
    bool checkAndCreateExperimentTable(QSqlDatabase *experimentDatabase);

    // 插入数据库
    bool insertParticleIntoDatabase(FlyFireParticle *particle, QSqlDatabase *database);
    bool insertSampleIntoDatabase(SampleInfo *sample, QSqlDatabase *database);
    bool insertExperimentIntoDatabase(ExperimentInfo *experiment, QSqlDatabase *database);

    // 全表显示
    void showAllFhklInTableView(QSqlDatabase *database);
    void showAllSampleInTableView(QSqlDatabase *database);
    void showAllExperimentInTableView(QSqlDatabase *database);

    // 数据维护菜单
    void fhkl_Menu();
    void sample_Menu();
    void experiment_Menu();

    QString dealImagePath(const QString &filePath);
};

#endif // FHMAINWINDOW_H
